home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest1_0.lha / Tests / exer / cvar.C < prev    next >
C/C++ Source or Header  |  1991-12-11  |  2KB  |  88 lines

  1. #include <stdio.h>
  2. #include <stream.h>
  3. #include "presto.h"
  4.  
  5. shared_t int verbose = 0;
  6.  
  7. int
  8. Main::init()
  9. {
  10.     nummainthreads = 1;
  11.     for (argc--, argv++; *argv && **argv == '-'; argv++, argc--)
  12.         switch (*(*argv + 1)) {
  13. #ifdef sequent
  14. #ifdef i386
  15.                 case 'a':
  16.                         affinity = 1;
  17.                 break;
  18. #endif /* i386 */
  19. #endif /* sequent */
  20.             case 'q':
  21.                 quantum = atoi(*argv + 2);
  22.                 break;
  23.             case 'n':
  24.                 numprocessors = atoi(*argv + 2);
  25.                 break;
  26.             case 't':
  27.                 nummainthreads = atoi(*argv + 2);
  28.                 break;
  29.             case 'v':
  30.                 verbose = 1;
  31.                 break;
  32.             default:
  33.                 cerr << chr(*(*argv + 1)) << " unknown flag\n";
  34.                     return -1;
  35.         }
  36.     return 0;
  37. }
  38.  
  39.  
  40. static shared_t Monitor m("main_monitor");
  41. static shared_t Condition c(&m, "condition");
  42. static shared_t int awakened = 0;
  43. static shared_t int occupancy = 0;
  44.  
  45. int
  46. Main::main()
  47. {
  48.  
  49.     m.entry();
  50.         occupancy++;
  51.         if (occupancy == 1)
  52.             if (verbose)
  53.                 printf("%s: thread ID %d started\n", 
  54.                 thisproc->name(), thisthread->tid());
  55.         if (occupancy == nummainthreads) {
  56.             if (verbose)
  57.                 printf("Occupant #%d broadcasting (%s.%d:%s)\n", 
  58.                 occupancy, thisproc->name(),
  59.                 thisthread->tid(), thisthread->name());
  60.  
  61.             awakened = 1;
  62.             c.broadcast();
  63.         } else    {
  64.             if (verbose)
  65.                 printf("Occupant #%d sleeping (%s.%d:%s)\n", 
  66.                 occupancy, thisproc->name(),
  67.                 thisthread->tid(), thisthread->name());
  68.             c.wait();    // release monitor
  69.             if (!awakened) {
  70.                 printf("Failure.\n");
  71.                 abort();
  72.             }
  73.             if (verbose)
  74.                 printf("Occupant #%d waking (%s.%d:%s)\n", 
  75.                 occupancy, thisproc->name(),
  76.                 thisthread->tid(), thisthread->name());
  77.         }
  78.         occupancy--;
  79.     m.exit();
  80. }
  81.  
  82. int
  83. Main::done()
  84. {
  85.     printf("Success.\n");
  86.     return 0;
  87. }
  88.